//----------------------------------------------------------------------组合数取模

//need prime table,and n < N
int combination_Mod_h (int n, int m, int h)
{
	if(h == 1) return 0;
        int result = 1, cnt = 0, temp;
	for(int i = 1; i < prime[0] && prime[i] <= n; i++)
	{
		temp = n, cnt = 0;
		while(temp)
			temp /= prime[i], cnt += temp;
		temp = n - m;
		while(temp)
			temp /= prime[i], cnt -= temp;
		temp = m;
		while(temp)
			temp /= prime[i], cnt -= temp;
		temp = prime[i];
		while(cnt)//mod_exp
		{
			if(cnt & 1)
                                result *= temp, result %= h;
			temp *= temp, cnt >>= 1, temp %= h;
		}
		if(result == 0) return 0;
	}
	return result;
}

///O(m) p is prime number, p*p < INT_MAX
int combination_Mod_p (int n, int m, int p)
{
    if (m > n) return 0;
    m = (n - m < m) ? n - m : m;
    int a = 1, b = 1, x, y, pcnt = 0;
    for (int i = 1; i <= m; i++)
    {
        a *= n - i + 1, b *= i;
        while (a % p == 0) a /= p, pcnt++;
        while (b % p == 0) b /= p, pcnt--;
        b %= p, a %= p;
    }
	if (pcnt) return 0;
    ext_gcd(b, p, x, y);
    if (x < 0) x += p;
    x *= a, x %= p;
    return x;
}

/**
  * Lucas' theorem---combination number mod prime p
  * Be sure that p*p<INT_MAX
  **/
int lucas (int m, int n, int p)
{
    int result = 1;
    while (m && n && result)
        result *= combination_Mod_p (m % p, n % p, p), result %= p, m /= p, n /= p;//combinnation_Mod_h() works,too
    return result;
}
